﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace WindowsGame1.Controller
{
    class GameController : IController
    {
        //fält
        private View.GameView       _view;
        private Model.Model         _model;
        private View.Camera         _camera = new View.Camera();
        private View.Sound          _sound;
        private bool                _changeController = false;
        private bool                _nextLevel = false;

        public GameController(GraphicsDeviceManager a_manager, ContentManager a_contentManager)
        {
            _view = new View.GameView(a_manager, a_contentManager);
            _model = new Model.Model();
            _sound = new View.Sound(a_contentManager);
            _model.InitializeLevel();
            
        }

        public void Draw(float a_elapsedTime, Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice)
        {
            Model.Level level = _model.GetLevel();

            //ställer in kameran att sikta på spelaren
            _camera.CenterOn(_model.GetPlayerPosition(),
                              new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),
                              new Vector2(Model.Level.g_levelWidth, Model.Level.g_levelHeight));

            //ställer in zoomen 
            _camera.SetZoom(48);

            //ritar upp banan..
            _view.DrawGame(GraphicsDevice, level, _camera, _model.GetEnemyPositions(), _model.GetEnemyMinePositions(), _model.GetCarrotPositions(),
                           _model.GetPlayerPosition(), _model.GetStalkerPositions(), _model.GetPlayerHealth(), a_elapsedTime);
        }

        public bool DidUserChangeController()
        {
            bool ret = _changeController;
            _changeController = false;
            return ret;
        }

        public void Update(float a_elapsedTime)
        {

            if (_view.DidPlayerPressEsc() == true)
            {
                _changeController = true;
            }

            if (_nextLevel == true)
            {

                int currentLevel = _model.GetCurrentLevel();

                _model = new Model.Model();

                this._model.NextLevel(currentLevel + 1);

                _model.InitializeLevel();

                _nextLevel = false;

                _view.StartPlayingLevel();
            }

            if (_model.HasPlayerDied() == true || 
                _model.HasPlayerWonLevel() == true || 
                _model.HasPlayerWonGame() == true ||
                _model.StalkerHurtBox() == true)
            {
                if (_model.StalkerHurtBox() == true)
                {
                    _view.GameOver();
                }
                else
                {
                    if (_model.HasPlayerWonGame() == true)
                    {
                        _view.PlayerWonGame();
                    }
                    else
                    {
                        if (_model.HasPlayerWonLevel() == true)
                        {
                            _view.PlayerWonLevel();
                        }
                        if (_model.HasPlayerDied() == true)
                        {
                            _view.PlayerDied();
                        }
                    }
                }
            }

            else
            {
                _model.Update(a_elapsedTime);
            }


            if (_view.DidPlayerPressEnter())
            {
                if (_model.HasPlayerWonLevel() == true)
                {
                    _nextLevel = true;
                }
            }

            if (_model.DidPlayerJumpOnMine())
            {
                _sound.PlayExplodingSound();
                _model.StopJumpOnMine();
            }

            if (_model.DidPlayerEatCarrot() == true)
            {
                _sound.PlayEatingCarrotSound();
                _model.StopEatingCarrot();
            }

            //react to input
            if (_view.DidPlayerPressJump())
            {
                if (_model.CanJump() && _model.HasPlayerDied() == false)
                {
                    _model.DoJump();
                    _sound.PlayJumpSound();
                }
            }
            //kollar om man ska stanna rörelsen så man inte har en massa glid när man spelar
            if (_view.StopVelocity() == true)
            {
                _model.StopMovement();
            }
            else
            {
                if (_view.DidPlayerPressRight())
                {
                    //kollar regler om man kan gå till höger
                    if (_model.CanGoRight() == true && _model.HasPlayerDied() == false)
                    {
                        _model.GoRight();
                        //vänter spelaren mot höger
                        _view.TurnPlayerRight();
                    }
                }
                if (_view.DidPlayerPressLeft())
                {
                    //kollar regler om man kan gå mot vänster
                    if (_model.CanGoLeft() == true && _model.HasPlayerDied() == false)
                    {
                        _model.GoLeft();
                        //vänter spelaren till vänster
                        _view.TurnPlayerLeft();
                    }
                }
            }
        }
    }
}
